package com.uking.ukingmodule;
import android.util.Log;

import com.smalls.newvideotwo.NewDTVApp;
import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.utils.Constant;

public class ukingjnic {
	private volatile static ukingjnic instance;
	private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "ukingjnic":"ukingjnic";
	private String RETURN_ID;
	private int PORT;

	public static ukingjnic getInstance() {
		if(instance == null){
			synchronized (ukingjnic.class) {
				if(instance == null){
					instance = new ukingjnic();
				}
			}
		}
		return instance;
	}
	private ukingjnic(){
	 }
	static{
		System.loadLibrary("c++_shared");
		System.loadLibrary("UKing_STBProxy");
	}



	private native String native_activateauth(String authserverip, String authserverdomain,String apktype,String activatecode,String mac,String mac1,int debug);

	//兼容线上的版本
	private native String native_registerauth(String authserverip, String authserverdomain, String apktype,String path,String token,int debug);

	private native String native_loginauth(String authserverip, String authserverdomain, String apktype,String path,String uid,int debug);
	private native String native_newmodule(Object weak_thiz,int type,int playport,int debug);
	private native String native_codeauthentication(int type, String code);
	private native String native_getplayurl(int type, String code,String path);
	private native String native_startdownload(int type, String code,String path);
	private native String native_closecontent(int type, String code);
	private native String native_releasemodule(int type);

	/**
	 * @param module_ref 为操作模块类对象
	 * @param msgwhat 为传递消息类型
	 * @param msgContent 消息内容
	 * */
	
	@SuppressWarnings("unchecked")
	public static void postEventFromNative(Object module_ref, int msgwhat, String msgContent) {
		//ukingjnic mp = (ukingjnic) ((WeakReference<Object>) module_ref).get();
        LogUtil.i(TAG, "msgWhat="+msgwhat+",msgContent="+ msgContent);
        ukingjnic mp = (ukingjnic) module_ref;
		if (mp == null) {
			LogUtil.i(TAG, "mp is null");
			return;
		}
		mp.handle_events(msgwhat, msgContent);
	}
 	
	public void handle_events(int what, String msgContent)
			throws IllegalStateException {
		if (monStateChangeLisenser != null)
			monStateChangeLisenser.StateChanged(what, msgContent);
		else
			LogUtil.i(TAG, "monStateChangeLisenser is null");
		return;
	}
	
	private onStateChangeLisenser monStateChangeLisenser;

	public interface onStateChangeLisenser {
		void StateChanged(int msg, String msgContent);
	}

	public void setonStateChangeLisenser(onStateChangeLisenser listener) {
		monStateChangeLisenser = listener;
	}

	/**
	 @brief 中间件补充其他参数访问AAA进行激活,激活结果为一个uid,与APK+STB终身绑定,除非再次激活
	 @param authserver---apk给出的3A地址
	 apksn       ---- apk发布名称(可以理解为类型)
	 activatecode  ---- 贴在stb上的激活码,用户在apk界面上输入
	 @return  &CODE=0&INFO=Success&USERID=c255b986-3cfe-11e8-95ad-6b785aa9ae3a&ACTIVATETM=2018-04-11,04:35:51
	 @note    同一个apk的不同版本升级,不需要 重新通过 该 接口激活,apk以后用该uid作为身份标识访问后续鉴权接口
	 */
	public String activateAuth(String authserver, String apksn,String activatecode,String mac,String mac1,int debug)  {
		return native_activateauth(authserver, authserver,apksn,activatecode,mac,mac1,debug);

	}


	/**
	 * 向AAA注册用户
	 * 
	 * @param authserver
	 *            AAA服务器地址
	 *            stb sn号
	 * @param uid
	 *            激活得到的uid字符串(与APK+STB终身绑定,除非再次激活)
	 * @param apksn
	 *           apk发布名称(可以理解为类型)
	 * @return
	 */
	public String registerUser(String authserver, String uid, String apksn,int debug) {
		return native_loginauth(authserver, authserver, apksn,NewDTVApp.getApp().getFilesDir().getAbsolutePath(),uid,debug);

	}
	/**
	 * 初始化
	 * 
	 * @param type
	 *            1 为点播 ， 2 为 直播
	 * @param playport
	 * @return
	 * @throws IllegalStateException
	 */
	public String initDownloadModul(int type, int playport)
			throws IllegalStateException {

		PORT = playport;
		RETURN_ID = native_newmodule(this,type, playport,1);

		return RETURN_ID;
	}

	/**
	 * 功能 鉴权code 是否可以播放this
	 * 
	 * @param type
	 *            直播为2，点播为1
	 * @param code
	 *            播放的mediacode
	 * @return
	 */
	public String playCodeAuthentication(int type,String code) {

		return native_codeauthentication(type, code);

	}
	/**
	 * 功能： 获取播放链接
	 */
	public String startGetPlayURL(int type, String code)
			throws IllegalStateException {
		return native_getplayurl(type, code,NewDTVApp.getApp().getFilesDir().getAbsolutePath());
	}
	/**
	 * 功能： 下载数据
	 */
	public String startDownload(int type, String code)
			throws IllegalStateException {
		return native_startdownload(type, code, NewDTVApp.getApp().getFilesDir().getAbsolutePath());
	}
	/**
	 * 功能：停止当前下载内容
	 * 
	 * @param type
	 *            直播为1，点播为2
	 * @return
	 */
	public String stopDownload(int type,String code) {
		return native_closecontent(type,code);
	}

	/**
	 * 
	 * @param type
	 *            1为直播，2为点播
	 * @return 失败返回负值
	 */
	public String destroyDlownloadModul(int type) {

		String result = native_releasemodule(type);
		return result;
	}

}
